<template>
  <el-dialog v-model="visible" :title="!dataForm.cusId ? '新增' : '修改'" :close-on-click-modal="false"
             :close-on-press-escape="false" width="800">
    <el-form :model="dataForm" :rules="rules" ref="dataFormRef" @keyup.enter="dataFormSubmitHandle()"
             label-width="120px">
      <el-form-item label="客户姓名" prop="cusName">
        <el-input v-model="dataForm.cusName" placeholder="客户姓名"  style="width: 400px;"></el-input>
      </el-form-item>
      <el-form-item label="生日" prop="birth">
        <el-date-picker
          v-model="dataForm.birth"
          type="datetime"
          placeholder="Pick a day"
          value-format="YYYY-MM-DD HH:mm:ss"
        />
      </el-form-item>
      <el-form-item label="手机号" prop="phonenum">
        <el-input v-model="dataForm.phonenum" placeholder="手机号" style="width: 400px;"></el-input>
      </el-form-item>
      <el-form-item label="身高" prop="height">
        <el-input type="number" v-model="dataForm.height" placeholder="身高" style="width: 100px;"></el-input>(cm)
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="dataForm.status" placeholder="状态 0：正常，1封禁" style="width: 240px">
          <el-option
            v-for="item in [{value: 1,label: '封禁',},{value: 0,label: '正常',}]"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="性别" prop="gender">
        <el-select v-model="dataForm.gender" placeholder="性别 0：男，1：女，2：保密" style="width: 240px">
          <el-option
            v-for="item in [{value: 0,label: '男',},{value: 1,label: '女',},{value: 2,label: '保密',}]"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
    </el-form>
    <template #footer>
      <div  style="width: 350px">
        <el-button @click="visible = false">取消</el-button>
        <el-button type="primary" @click="dataFormSubmitHandle()">确定</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script lang="ts" setup>
import {reactive, ref} from "vue";
import baseService from "@/service/baseService";
import {ElMessage} from "element-plus";

const emit = defineEmits(["refreshDataList"]);

const visible = ref(false);
const dataFormRef = ref();

const dataForm = reactive({
  cusId: '', cusName: '', birth: '', phonenum: '', height: '', status: 0, deleted: '', gender: '', insertTime: ''
});

const rules = ref({
  cusName: [
    {required: true, message: '必填项不能为空', trigger: 'blur'}
  ],
  birth: [
    {required: true, message: '必填项不能为空', trigger: 'blur'}
  ],
  phonenum: [
    {required: true, message: '必填项不能为空', trigger: 'blur'},
    {pattern:/^1([3456789])[0-9]\d{8}$/, message: '手机号格式错误', trigger: 'blur'}
  ],
  height: [
    {required: true, message: '必填项不能为空', trigger: 'blur'}
  ],
  status: [
    {required: true, message: '必填项不能为空', trigger: 'blur'}
  ],
  deleted: [
    {required: true, message: '必填项不能为空', trigger: 'blur'}
  ],
  gender: [
    {required: true, message: '必填项不能为空', trigger: 'blur'}
  ],
  insertTime: [
    {required: true, message: '必填项不能为空', trigger: 'blur'}
  ]
});

const init = (cusId?: number) => {
  visible.value = true;
  dataForm.cusId = "";

  // 重置表单数据
  if (dataFormRef.value) {
    dataFormRef.value.resetFields();
  }

  if (cusId) {
    getInfo(cusId);
  }
};

// 获取信息
const getInfo = (cusId: number) => {
  baseService.get("/health/hmcustomer/" + cusId).then((res) => {
    Object.assign(dataForm, res.data);
  });
};

// 表单提交
const dataFormSubmitHandle = () => {
  dataFormRef.value.validate((valid: boolean) => {
    if (!valid) {
      return false;
    }
    (!dataForm.cusId ? baseService.post : baseService.put)("/health/hmcustomer", dataForm).then((res) => {
      ElMessage.success({
        message: '成功',
        duration: 500,
        onClose: () => {
          visible.value = false;
          emit("refreshDataList");
        }
      });
    });
  });
};

defineExpose({
  init
});
</script>
